package com.yss.system.service.impl;

import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yss.system.domain.SysUserPost;
import com.yss.system.service.ISysUserPostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yss.common.core.constant.UserConstants;
import com.yss.common.core.exception.BusinessException;
import com.yss.common.core.utils.StringUtils;
import com.yss.system.domain.SysPost;
import com.yss.system.mapper.SysPostMapper;
import com.yss.system.service.ISysPostService;

/**
 * 岗位信息 服务层处理
 */
@Service
public class SysPostServiceImpl extends ServiceImpl<SysPostMapper, SysPost> implements ISysPostService {
    @Autowired
    private SysPostMapper postMapper;

    @Autowired
    private ISysUserPostService userPostService;

    /**
     * 查询岗位信息集合
     *
     * @param post 岗位信息
     * @return 岗位信息集合
     */
    @Override
    public List<SysPost> selectPostList(SysPost post) {
        QueryWrapper<SysPost> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("CREATE_TIME" );
        if (null == post) {
            this.list(wrapper);
        }
        if (StringUtils.isNotEmpty(post.getPostCode())) {
            wrapper.like("POST_CODE" , post.getPostCode());
        }
        if (null != post.getStatus()) {
            wrapper.like("STATUS" , post.getStatus());
        }
        if (StringUtils.isNotEmpty(post.getPostName())) {
            wrapper.like("POST_NAME" , post.getPostName());
        }
        return this.list(wrapper);
    }

    /**
     * 根据用户ID获取岗位选择框列表
     *
     * @param userId 用户ID
     * @return 选中岗位ID列表
     */
    @Override
    public List<String> selectPostListByUserId(String userId) {
        return postMapper.selectPostListByUserId(userId);
    }

    /**
     * 校验岗位名称是否唯一
     *
     * @param post 岗位信息
     * @return 结果
     */
    @Override
    public boolean checkPostNameUnique(SysPost post) {
        String id = StringUtils.isNull(post.getId()) ? "-1" : post.getId();

        QueryWrapper<SysPost> wrapper = new QueryWrapper<>();
        wrapper.eq("POST_NAME" , post.getPostName());
        SysPost info = this.getOne(wrapper);
        if (StringUtils.isNotNull(info) && !info.getId().equals(id)) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 校验岗位编码是否唯一
     *
     * @param post 岗位信息
     * @return 结果
     */
    @Override
    public boolean checkPostCodeUnique(SysPost post) {
        String id = StringUtils.isNull(post.getId()) ? "-1" : post.getId();
        QueryWrapper<SysPost> wrapper = new QueryWrapper<>();
        wrapper.eq("POST_CODE" , post.getPostName());
        SysPost info = this.getOne(wrapper);
        if (StringUtils.isNotNull(info) && !info.getId().equals(id)) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 通过岗位ID查询岗位使用数量
     *
     * @param id 岗位ID
     * @return 结果
     */
    @Override
    public Long countUserPostById(String id) {
        QueryWrapper<SysUserPost> wrapper = new QueryWrapper<>();
        wrapper.eq("POST_ID" , id);
        return userPostService.count(wrapper);
    }

    /**
     * 批量删除岗位信息
     *
     * @param ids 需要删除的岗位ID
     * @return 结果
     */
    @Override
    public int deletePostByIds(String[] ids) {
        for (String id : ids) {
            SysPost post = this.getById(id);
            if (this.countUserPostById(id) > 0) {
                throw new BusinessException(String.format("%1$s已分配,不能删除" , post.getPostName()));
            }
        }
        List<String> list = Arrays.asList(ids);
        return postMapper.deleteBatchIds(list);
    }
}
